/** 查询功能 */
import {
    processingData,
    addResultCells
} from './faceView.js';
var pageNo = 1,
    pageSize = 20;
var startDate, endDate;
var detailphone;
// 查询初始化
function initQuery() {

    console.log("initQuery");
    $("#start-date").calendar({
        onChange: function(p, values, displayValues) {
            startDate = (new Date(values[0])).setHours(0);
            console.dir(p, values, displayValues);
        }
    });

    $("#end-date").calendar({
        onChange: function(p, values, displayValues) {
            endDate = (new Date(values[0])).setHours(23);
            console.dir(p, values, displayValues);
        }
    });

    $("#query-record").on("click", function() {
        pageNo = 1;
        queryRecord();
    });
    $("#record-next_page").on("click", function() {
        if ($(this).hasClass("noNext")) {

        } else {
            pageNo++;
            queryRecord();
        }


    });

    /**********绑定触碰且移动不触发;**********/
    var moved = false;

    $("#record-list").on("touchmove", ".weui-cells", function() {
        moved = true;
    });
    $("#record-list").on("touchend", ".weui-cells", function() {
        if (!moved) {
            var theid = $(this).data("id");
            var status = $(this).data("status");
            detailphone = $(this).data("phone");
            if (theid != "" && theid != "null"  && theid != null && theid != "undefind") {
                $.post(logDetailServlet, {
                    id: theid
                }, showDetail);
            } else {
                $.alert("查询失败", "查询结果");
            }
        }
        moved = false;
    });

}

// 填写基本信息
function fillBaseInfo(detailData) {
    var isFillName = true,
        isFillId = true,
        isFillPhoneNo = true;
    if (detailphone) {
        $("#step4-phone").text(detailphone);
    }
    for (var i = detailData.length - 1, l = 0; i >= l; i--) {
        if (detailData[i].name && isFillName == true) {
            $("#step4-name").text(detailData[i].name);
            isFillName = false;
        }
        if (detailData[i].idCard && isFillId == true) {
            $("#step4-sfz").text(detailData[i].idCard);
            isFillId = false;
        }
        if (detailData[i].phoneNo && isFillPhoneNo == true) {
            $("#step4-phone").text(detailData[i].phoneNo);
            isFillPhoneNo = false;
        }
    }
}

// 显示记录详情
function showDetail(sdata) {
    $("#step0").hide();
    $("#step4").show();
    $("#validate-result").empty();
    if (sdata && sdata.length) {
        fillBaseInfo(sdata);
        for (var i = sdata.length - 1, l = 0; i >= l; i--) {
            processingData(sdata[i]);
        }
    }

}

// 查询记录列表
function queryRecord() {
    var username = $("#name").val();
    var paraments = {
        pageNo: pageNo,
        pageSize: 20, //pageSize,
        startDate: startDate,
        endDate: endDate,
        //            operatorPhone: operatorPhone,
        userName: username,
        pkgId: "001"
    };
    $.post(queryRecordServlet, paraments, recordBack);
}

// 记录列表得到后创建html
function recordBack(rdata) {
    console.dir(rdata);
    var recordHtml = '';
    var theData = rdata.data;
    if (theData && theData.length > 0) {
        for (var i = 0, l = theData.length; i < l; i++) {
            var formartDate = getDateFormate("yyyy-mm-dd", new Date(theData[i].insertime));
            var resutlSmg = "查询失败";
            var resultClass = "query-fail";
            if (theData[i].queryStatus == "1") {
                resutlSmg = "查询成功";
                resultClass = "query-succeed";
            }

            recordHtml += `
<div class="weui-cells" data-id="${theData[i].id}" data-phone="${theData[i].phone}" data-status="${theData[i].queryStatus}">
    <div class="weui-cell">
        <div class="weui-cell__hd"><img class="libiao-icon" src="../img/faceDetect/libiao_iconpx.png"></div>
        <div class="weui-cell__bd">
            <p>${theData[i].name}</p>
        </div>
        <div class="weui-cell__ft">${formartDate}</div>
    </div>
    <div class="weui-cell vice-cell">
        <div class="weui-cell__hd"></div>
        <div class="weui-cell__bd">
            <p>组合A</p>
        </div>
        <div class="weui-cell__ft ${resultClass}">${resutlSmg}</div>
    </div>
</div>
`;

        }
    } else {


    }
    if (pageNo >= rdata.totalPage) {
        $("#record-next_page").addClass("noNext").text("没有更多记录");
    } else {
        $("#record-next_page").removeClass("noNext").text("加载更多记录");
    }

    if (pageNo == 1) {
        $("#record-list").html(recordHtml);
    } else {
        $("#record-list").append(recordHtml);
    }
    $("#next-pate_box").show();
}

// 转换时间
function getDateFormate(formart, newDate) {
    // format: "yyyy-mm-dd hh:ii:ss"  | "yyyy-mm-dd"
    var dateSty = arguments[0].split(' '),
        separatorFirst = '',
        separatorSecond = '',
        realDate = '';
    var yyyy = newDate.getFullYear(),
        mm = newDate.getMonth() + 1,
        dd = newDate.getDate(),
        hh = newDate.getHours(),
        ii = newDate.getMinutes(),
        ss = newDate.getSeconds();
    mm = mm < 10 ? '0' + mm : mm;
    dd = dd < 10 ? '0' + dd : dd;
    hh = hh < 10 ? '0' + hh : hh;
    ii = ii < 10 ? '0' + ii : ii;
    ss = ss < 10 ? '0' + ss : ss;

    if (dateSty.length > 1) {
        //  console.log(dateSty);
        separatorFirst = dateSty[0].charAt(4);
        separatorSecond = dateSty[1].charAt(2);
        realDate = yyyy + separatorFirst + mm + separatorFirst + dd + ' ' + hh + separatorSecond + ii + separatorSecond + ss;
    } else {
        separatorFirst = dateSty[0].charAt(4);
        realDate = yyyy + separatorFirst + mm + separatorFirst + dd;
    }

    return realDate;
}
module.exports = {
    test: function() {
        console.log("weewew");
    },
    initQuery: initQuery

}
